<?php 
if (!defined('BASEPATH'))exit('No direct script access allowed');

/**
 *  Siap commit 
 */ 
class admin extends Site_My_Controller 
{
    private $templates = array("login_a", "login_b", "login_c", "login_d", "login_e");
    private $template = "login_a";
    
    public function __construct() 
    {
        parent::__construct();
    }

    public function index() 
    {
        
        $data = array();
        $data['var_page'] = "signin";
        
        $this->PAGE_TITLE = "Sign In";

        $this->globallib->set_language("id");
        $this->userauth->__init();

        $mode = $this->input->get_post("m");
        $self = explode("/", $_SERVER['SCRIPT_NAME']);
        
        $self = (isset($data['site']['url'])?$data['site']['url']:"") . $self[count($self) - 1];

        if ($mode == 'logout') 
        {
            $this->userauth->logout();
        }
        $is_logged = $this->userauth->is_logged();
        if (!$is_logged) 
        {
            if ($data['var_page'] != "signin") 
            {
                echo "masuk";
               
                
                $this->globallib->addMsg($this->userauth->error_string);
                $this->load->view($this->template, $data);
                
            }
            else
            {
                
                $this->login($is_logged, "");
               
            }
        } 
        else 
        {
            $this->login($is_logged, "");
        }
    }

    public function logout() 
    {
        $this->userauth->logout();
        $this->login();
    }
    
    public function login_a()
    {
        $this->load->helpers("url");
        $this->load->view($this->template);
    }

    public function login($logged_status = null, $logout = "") 
    {
        
        $this->load->helpers("url");
        
        if ($logged_status || $this->userauth->is_logged()) 
        {
            $this->home();
        } 
        else 
        {
            $data = array();
            $data['userID'] = $this->userauth->getUserID();
            $data['name'] = $this->userauth->getName();
            $data['site'] = $this->generalfunction->getDir();
            $data['islogged'] = false;
            
            
            $mode = $logout;
            $validate = $this->input->get_post('v');
            $pageredirection = isset($_SERVER["QUERY_STRING"])?$this->encrypt->decode($_SERVER["QUERY_STRING"]):$this->input->get_post('pageredirection');
            
            if ($mode == "logout")
            {
                $this->userauth->logout();
            }
            
            if ($this->input->get_post('msg') == "off")
            {
                $this->globallib->addMsg("You have been logout.");
            }
            
            if ($this->input->get_post("from_post")) 
            {
                
                $username = $this->globallib->request("username");

                if ($username == "")
                {
                    $username ='0';
                }

                $password = md5($this->globallib->request("password"));

                if ($password == "")
                {
                    $this->globallib->fieldErr("password");
                }
                
                $loginst = $this->userauth->loginproses($username, $password);
                
                if (!$this->globallib->isErr()) 
                {
                    if ($loginst) 
                    {
                        $_SESSION["fl"] = true;
                            $_SESSION["loginpageredirection"] = $pageredirection;
                            
                            echo "1";
                    } 
                    else 
                    {
                        $this->globallib->fieldErr("username", ( $this->userauth->error_string ) ? $this->userauth->error_string : "Login process failed, please try again...", false);
      
                        echo $this->globallib->errorStatusPopup();
                    }
                }
                else
                {
                    $this->load->view($this->template, $data);
                }
            }
            else
            {
                $this->load->view($this->template, $data);
            }
        }
    }

    public function home($data = '') 
    {
        $datamenu = $this->getMenu();
        $datamenu['pageTitle'] = "Home Anugerah Group Management System";
        
                
        $this->load->view('template/header', $datamenu);
        $this->load->view('template/menu', $datamenu);
        $this->load->view('template/name', $datamenu);
        $this->load->view('admin/index', $data);
        $this->load->view('template/footer');
    }
    
    public function not_found() 
    {
        $datamenu = $this->getMenu();
        $datamenu['pageTitle'] = "Home Anugerah Group Management System";
        
                
        $this->load->view('template/header', $datamenu);
        $this->load->view('template/menu', $datamenu);
        $this->load->view('template/name', $datamenu);
        $this->load->view('admin/not_found');
        $this->load->view('template/footer');
    }
    
    function documentation_edit($id_menu = 0)
    {
        $content = $this->input->post("isi");
        
        $this->db->where("id_menu", $id_menu);
        $flag = $this->db->update("mt_menu", array("dokumentasi_teknis_singkat" => $content));
        
        if($flag)
        {
            echo "1";
        }
        else
        {
            echo "0";
        }
    }
    
}

?>
